package com.coldchain.hz.service.impl;

import com.coldchain.common.msg.BaseResponse;
import com.coldchain.common.msg.ObjectRestResponse;
import com.coldchain.hz.entity.ReceivedAddr;
import com.coldchain.hz.mapper.ReceivedAddrMapper;
import com.coldchain.hz.service.IReceivedAddrService;
import com.fangliai.core.base.service.impl.BaseServiceImpl;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Service;

import java.util.Date;


/**
 * <p>
 * 冷链货主发货地址 服务实现类
 * </p>
 *
 * @author huangzm
 * @since 2018-08-29
 */
@Service
public class ReceivedAddrServiceImpl extends BaseServiceImpl<ReceivedAddrMapper, ReceivedAddr> implements IReceivedAddrService {
	private static final Logger log= LoggerFactory.getLogger(ReceivedAddrServiceImpl.class);

	@Override
	public BaseResponse del(String uid, Integer id) {
		ReceivedAddr addr=getById(id);
		if(addr.getCompanyId().equals(uid)){
			try{
				removeById(id);
				return new ObjectRestResponse<>().succeed("ok");
			}catch (Exception e){
				log.error("del address  error.{}",e.getMessage());
				return new ObjectRestResponse<>().error("服务器异常");
			}
		}else {
			return new ObjectRestResponse<>().error("非法请求");
		}

	}

	@Override
	public BaseResponse edit(String uid, ReceivedAddr receivedAddr) {
		if(receivedAddr.getId()==null){
			receivedAddr.setCreateTime(new Date());
			receivedAddr.setCompanyId(uid);
		}
		try {
			saveOrUpdate(receivedAddr);
			return new ObjectRestResponse<>().succeed("ok");
		}catch (Exception e){
			log.error("edit address  error.{}",e.getMessage());
			return new ObjectRestResponse<>().error("服务器异常");
		}
	}
}
